# Author    : CoffeeChicken
# Date      : 2020-08-04 23:21


import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QDesktopWidget, QComboBox, QLineEdit, QCheckBox, QPushButton
from PyQt5.QtGui import QFont
from PyQt5.QtCore import Qt
import getpass
import os


class Center(QWidget):

    def __init__(self):
        super().__init__()
        self.information_label = None
        self.title_label = None
        self.theme_label = None
        self.theme_combo = None
        self.type_label = None
        self.type_combo = None
        self.chart_name_label = None
        self.chart_name_input = None
        self.x_name_label = None

        self.information_label = None

        self.init_window()

    def init_window(self):
        # 添加软件标题
        self.title_label = QLabel(self)
        self.title_label.setText("Plot a Wonderful Chart")
        self.title_label.setFont(QFont("Apple Chancery", 30, QFont.Bold))
        self.title_label.setAlignment(Qt.AlignCenter)
        self.title_label.move(230, 10)

        # 添加主题
        self.theme_label = QLabel(self)
        self.theme_combo = QComboBox(self)
        self.theme_combo.addItems(
            ["DefaultStyle", "DarkStyle", "NeonStyle", "DarkSolarizedStyle", "LightSolarizedStyle",
             "LightStyle", "CleanStyle", "LightColorizedStyle", "DarkColorizedStyle", "TurquoiseStyle",
             "LightGreenStyle", "DarkGreenStyle", "DarkGreenBlueStyle", "BlueStyle", "RedBlueStyle"])
        self.theme_label.setText("theme: ")
        self.theme_label.setFont(QFont("Chalkboard", 18, QFont.Bold))
        self.theme_combo.setFont(QFont("Chalkboard", 18))
        self.theme_label.move(95, 102)
        self.theme_combo.move(160, 100)

        # 添加图表类型
        self.type_label = QLabel(self)
        self.type_combo = QComboBox(self)
        self.type_combo.addItems(["Line", "Pie", "Bar"])
        self.type_label.setText("type: ")
        self.type_label.setFont(QFont("Chalkboard", 18, QFont.Bold))
        self.type_combo.setFont(QFont("Chalkboard", 18))
        self.type_label.move(450, 102)
        self.type_combo.move(510, 100)

        # 添加基本配置
        # 添加chart_name
        self.chart_name_label = QLabel(self)
        self.chart_name_label.setText("chart name: ")
        self.chart_name_label.setFont(QFont("Comic Sans MS", 18, QFont.Bold))
        self.chart_name_label.move(95, 160)
        self.chart_name_input = QLineEdit(self)
        self.chart_name_input.setFont(QFont("Comic Sans MS", 15))
        self.chart_name_input.move(220, 160)

        # 添加X_name
        self.x_name_label = QLabel(self)
        self.x_name_label.setText("X name: ")
        self.x_name_label.setFont(QFont("Comic Sans MS", 18, QFont.Bold))
        self.x_name_label.move(450, 140)
        self.x_name_input = QLineEdit(self)
        self.x_name_input.setFont(QFont("Comic Sans MS", 15))
        self.x_name_input.move(540, 140)

        # 添加Y_name
        self.y_name_label = QLabel(self)
        self.y_name_label.setText("Y name: ")
        self.y_name_label.setFont(QFont("Comic Sans MS", 18, QFont.Bold))
        self.y_name_label.move(450, 170)
        self.y_name_input = QLineEdit(self)
        self.y_name_input.setFont(QFont("Comic Sans MS", 15))
        self.y_name_input.move(540, 170)

        # 添加data series1
        self.data1_label = QLabel(self)
        self.data1_label.setText("data series1: ")
        self.data1_label.setFont(QFont("Chalkboard", 22, QFont.Bold))
        self.data1_label.move(95, 220)
        self.data1_name_label = QLabel(self)
        self.data1_name_label.setText("data name: ")
        self.data1_name_label.setFont(QFont("Comic Sans MS", 18, QFont.Bold))
        self.data1_name_label.move(95, 260)

        self.data1_name_input = QLineEdit(self)
        self.data1_name_input.setFont(QFont("Comic Sans MS", 15))
        self.data1_name_input.move(220, 260)

        self.data1_x_label = QLabel(self)
        self.data1_x_label.setText("X data: ")
        self.data1_x_label.setFont(QFont("Comic Sans MS", 18, QFont.Bold))
        self.data1_x_label.move(95, 290)
        self.data1_x_input = QLineEdit(self)
        self.data1_x_input.setFont(QFont("Comic Sans MS", 15))
        self.data1_x_input.move(220, 290)

        self.data1_y_label = QLabel(self)
        self.data1_y_label.setText("Y data: ")
        self.data1_y_label.setFont(QFont("Comic Sans MS", 18, QFont.Bold))
        self.data1_y_label.move(95, 320)
        self.data1_y_input = QLineEdit(self)
        self.data1_y_input.setFont(QFont("Comic Sans MS", 15))
        self.data1_y_input.move(220, 320)

        # 添加data series2
        self.data2_label = QLabel(self)
        self.data2_label.setText("data series2: ")
        self.data2_label.setFont(QFont("Chalkboard", 22, QFont.Bold))
        self.data2_label.move(450, 220)

        self.data2_name_label = QLabel(self)
        self.data2_name_label.setText("data name: ")
        self.data2_name_label.setFont(QFont("Comic Sans MS", 18, QFont.Bold))
        self.data2_name_label.move(450, 260)
        self.data2_name_input = QLineEdit(self)
        self.data2_name_input.setFont(QFont("Comic Sans MS", 15))
        self.data2_name_input.move(540, 260)

        # self.data2_x_label = QLabel(self)
        # self.data2_x_label.setText("X data: ")
        # self.data2_x_label.setFont(QFont("Comic Sans MS", 18, QFont.Bold))
        # self.data2_x_label.move(450, 290)
        # self.data2_x_input = QLineEdit(self)
        # self.data2_x_input.setFont(QFont("Comic Sans MS", 15))
        # self.data2_x_input.move(540, 290)

        self.data2_y_label = QLabel(self)
        self.data2_y_label.setText("Y data: ")
        self.data2_y_label.setFont(QFont("Comic Sans MS", 18, QFont.Bold))
        self.data2_y_label.move(450, 300)
        self.data2_y_input = QLineEdit(self)
        self.data2_y_input.setFont(QFont("Comic Sans MS", 15))
        self.data2_y_input.move(540, 300)

        # 添加data series3
        self.data3_label = QLabel(self)
        self.data3_label.setText("data series3: ")
        self.data3_label.setFont(QFont("Chalkboard", 22, QFont.Bold))
        self.data3_label.move(95, 380)
        self.data3_name_label = QLabel(self)
        self.data3_name_label.setText("data name: ")
        self.data3_name_label.setFont(QFont("Comic Sans MS", 18, QFont.Bold))
        self.data3_name_label.move(95, 420)
        self.data3_name_input = QLineEdit(self)
        self.data3_name_input.setFont(QFont("Comic Sans MS", 15))
        self.data3_name_input.move(220, 420)

        # self.data3_x_label = QLabel(self)
        # self.data3_x_label.setText("X data: ")
        # self.data3_x_label.setFont(QFont("Comic Sans MS", 18, QFont.Bold))
        # self.data3_x_label.move(95, 450)
        # self.data3_x_input = QLineEdit(self)
        # self.data3_x_input.setFont(QFont("Comic Sans MS", 15))
        # self.data3_x_input.move(220, 450)

        self.data3_y_label = QLabel(self)
        self.data3_y_label.setText("Y data: ")
        self.data3_y_label.setFont(QFont("Comic Sans MS", 18, QFont.Bold))
        self.data3_y_label.move(95, 460)
        self.data3_y_input = QLineEdit(self)
        self.data3_y_input.setFont(QFont("Comic Sans MS", 15))
        self.data3_y_input.move(220, 460)

        # 添加data series4
        self.data4_label = QLabel(self)
        self.data4_label.setText("data series4: ")
        self.data4_label.setFont(QFont("Chalkboard", 22, QFont.Bold))
        self.data4_label.move(450, 380)
        self.data4_name_label = QLabel(self)
        self.data4_name_label.setText("data name: ")
        self.data4_name_label.setFont(QFont("Comic Sans MS", 18, QFont.Bold))
        self.data4_name_label.move(450, 420)
        self.data4_name_input = QLineEdit(self)
        self.data4_name_input.setFont(QFont("Comic Sans MS", 15))
        self.data4_name_input.move(540, 420)

        # self.data4_x_label = QLabel(self)
        # self.data4_x_label.setText("X data: ")
        # self.data4_x_label.setFont(QFont("Comic Sans MS", 18, QFont.Bold))
        # self.data4_x_label.move(450, 450)
        # self.data4_x_input = QLineEdit(self)
        # self.data4_x_input.setFont(QFont("Comic Sans MS", 15))
        # self.data4_x_input.move(540, 450)

        self.data4_y_label = QLabel(self)
        self.data4_y_label.setText("Y data: ")
        self.data4_y_label.setFont(QFont("Comic Sans MS", 18, QFont.Bold))
        self.data4_y_label.move(450, 460)
        self.data4_y_input = QLineEdit(self)
        self.data4_y_input.setFont(QFont("Comic Sans MS", 15))
        self.data4_y_input.move(540, 460)

        # 添加几个单选框
        # self.value_checkbox = QCheckBox("show value", self)
        # self.value_checkbox.setFont(QFont("Comic Sans MS", 15))
        # self.value_checkbox.toggle()
        # self.value_checkbox.move(95, 530)
        #
        # self.legend_checkbox = QCheckBox("show legend", self)
        # self.legend_checkbox.setFont(QFont("Comic Sans MS", 15))
        # self.legend_checkbox.toggle()
        # self.legend_checkbox.move(220, 530)

        # 添加输出信息展示栏
        self.information_label = QLabel("                                                ", self)
        # self.information_label.setText("please check your data before push the button")
        self.information_label.setFont(QFont("Chalkboard", 18))
        self.information_label.move(95, 560)

        # 添加运行按钮
        self.run_button = QPushButton("Run", self)
        self.run_button.setFont(QFont("Impact", 30))
        self.run_button.move(330, 620)
        self.run_button.clicked.connect(lambda: self.run_code())
        # run_button.clicked().connect(self.run_code())

        # 设置基本窗口的参数
        self.setWindowTitle('画个吊图')
        self.setFixedSize(800, 700)
        self.show()

    # 将窗口居中
    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def run_code(self):
        self.information_label.setText("run good")
        theme = self.theme_combo.currentText()
        type_ = self.type_combo.currentText()
        chart_name = self.chart_name_input.text()
        x_name = self.x_name_input.text()
        y_name = self.y_name_input.text()
        data1_name = self.data1_name_input.text()
        data1_x = self.data1_x_input.text()
        data1_y = self.data1_y_input.text()

        data2_name = self.data2_name_input.text()
        # data2_x = self.data2_x_input.text()
        data2_y = self.data2_y_input.text()

        data3_name = self.data3_name_input.text()
        # data3_x = self.data3_x_input.text()
        data3_y = self.data3_y_input.text()

        data4_name = self.data4_name_input.text()
        # data4_x = self.data4_x_input.text()
        data4_y = self.data4_y_input.text()

        # print(type_)
        code = "import pygal\n"
        code += "from pygal import Config\n"
        code += "from pygal.style import " + theme + "\n"
        code += "config = Config()\n" \
                "config.print_values = True\n" \
                "config.print_values_position = \"top\"\n" \
                "config.style = " + theme + "\n" \
                "config.x_title = \"" + x_name + "\"\n" \
                "config.y_title = \"" + y_name + "\"\n"
        code += theme + ".value_font_size = 10\n"
        code += "chart = pygal." + type_ + "(config=config)\n"
        code += "chart.title = \"" + chart_name + "\"\n"
        code += "chart.x_labels = " + str(data1_x.split(",")) + "\n"
        # code += "chart.add(\"" + data1_name + "\", " + str(data1_y.split(",")) + ")\n"
        code += "chart.add(\"" + data1_name + "\", [" + data1_y + "])\n"
        if data2_name != "":
            code += "chart.add(\"" + data2_name + "\", [" + data2_y + "])\n"
        if data3_name != "":
            code += "chart.add(\"" + data3_name + "\", [" + data3_y + "])\n"
        if data4_name != "":
            code += "chart.add(\"" + data4_name + "\", [" + data4_y + "])\n"

        code += "chart.render_to_file('/Users/wondering/Desktop/" + chart_name + ".svg')\n"
        print(code)

        if chart_name == "":
            self.information_label.setText("Error! please input chart_name")
        else:
            self.information_label.setText("No error")

        username = getpass.getuser()
        file_path = "/Users/" + username + "/Desktop/" + chart_name + ".py"
        with open(file_path, 'w') as file:

            file.write(code)

        cmd = "python3 " + file_path
        pipe = os.popen(cmd)
        print(pipe.read())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    center = Center()
    sys.exit(app.exec_())
